package com.bootdo.collector.task;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bootdo.collector.domain.HotWords;
import com.bootdo.collector.utils.ESParse;
import com.bootdo.screen.domain.*;
import com.bootdo.screen.service.*;
import com.bootdo.screen.service.impl.DataclassesServiceImpl;
import org.apache.http.HttpEntity;
import org.apache.http.entity.ContentType;
import org.apache.http.nio.entity.NStringEntity;
import org.apache.http.util.EntityUtils;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.RestClient;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Collections;
import java.util.List;

@Component
public class TenSecondJob implements Job{

	@Autowired
    JdbcTemplate secondaryJdbcTemplate;
    //SimpMessagingTemplate template;
    @Autowired
    private HotwordtruenumService hotwordtruenumService;

    @Autowired
    private RestClient restClient;

    @Autowired
    private DataclassesService dataclassesService;

    @Autowired
    private BusinesstruenumService businesstruenumService;

    @Autowired
    private SourcetruenumService sourcetrueenumService;

    private final String ENERGYSEARCHURL ="energy_search/_search?size=0";
    private final  String OUTSOURCEURL ="sensitivity/doc/_search?size=0";
    private final String SEARCHSTRING ="{\"query\": {\"bool\": {\"should\": [ {\"match\": { \"title\": \"%s\" }},{\"match\": { \"content\": \"%s\" }} ]}}}";
    private  final String SOURCESTRING = "{\"query\": {\"term\": { \"cate_name\": { \"value\": \"%s\" } }}}";
    private  final String EMAILSTRING ="{\"query\": {\"match\": { \"domain\": \"mail.cnpc.com.cn\" }}}";

    @Override
    public void execute(JobExecutionContext arg0) throws JobExecutionException {

        String query;
        String res;

        List<HotWords> cphotwords = secondaryJdbcTemplate.query("select * from t_hot_words", new BeanPropertyRowMapper<>(HotWords.class));

        for (HotWords cphotword:cphotwords) {
            HotwordtruenumDO hotwordtruenumDO = new HotwordtruenumDO();
            hotwordtruenumDO.setId(cphotword.getId());
            hotwordtruenumDO.setNum(cphotword.getTimes());
            hotwordtruenumDO.setWord(cphotword.getKeyword());
            hotwordtruenumService.updateorsave(hotwordtruenumDO);
        }

        List<DataclassesDO> dataclasses = dataclassesService.list(null);
        for (DataclassesDO data: dataclasses) {
             query = String.format(SEARCHSTRING, data.getLevel3(), data.getLevel3());
             res = ESParse.execQuery(restClient,query,ENERGYSEARCHURL);
            data.setNum(ESParse.getTotal(ESParse.getfirsthits(res)));
            dataclassesService.update(data);
        }

        List<BusinesstruenumDO> businesses = businesstruenumService.list(null);

        for (BusinesstruenumDO business : businesses){
             query = String.format(SEARCHSTRING, "石油 oil", "石油 oil");
             res = ESParse.execQuery(restClient,query,ENERGYSEARCHURL);
            business.setOil(ESParse.getTotal(ESParse.getfirsthits(res)));

            query = String.format(SEARCHSTRING, "天然气 gas", "天然气 gas");
            res = ESParse.execQuery(restClient,query,ENERGYSEARCHURL);
            business.setGas(ESParse.getTotal(ESParse.getfirsthits(res)));

            query = String.format(SEARCHSTRING, "煤炭 coal", "煤炭 coal");
            res = ESParse.execQuery(restClient,query,ENERGYSEARCHURL);
            business.setCoal(ESParse.getTotal(ESParse.getfirsthits(res)));

            query = String.format(SEARCHSTRING, "电力", "电力");
            res = ESParse.execQuery(restClient,query,ENERGYSEARCHURL);
            business.setElectric(ESParse.getTotal(ESParse.getfirsthits(res)));

            query = String.format(SEARCHSTRING, "新能源", "新能源");
            res = ESParse.execQuery(restClient,query,ENERGYSEARCHURL);
            business.setAlternative(ESParse.getTotal(ESParse.getfirsthits(res)));
            businesstruenumService.update(business);
        }
        List<SourcetruenumDO> sources = sourcetrueenumService.list(null);
        for(SourcetruenumDO source :sources) {
            
             query = String.format(SOURCESTRING, "wechat");
             res = ESParse.execQuery(restClient,query,ENERGYSEARCHURL);
            source.setWechat(ESParse.getTotal(ESParse.getfirsthits(res)));

             query = String.format(SOURCESTRING, "trade");
             res = ESParse.execQuery(restClient,query,ENERGYSEARCHURL);
            source.setTrade(ESParse.getTotal(ESParse.getfirsthits(res)));

            query = String.format(SOURCESTRING, "open");
            res = ESParse.execQuery(restClient,query,ENERGYSEARCHURL);
            source.setInternet(ESParse.getTotal(ESParse.getfirsthits(res)));


            res = ESParse.execQuery(restClient,"{}",ENERGYSEARCHURL);
            source.setChangping(ESParse.getTotal(ESParse.getfirsthits(res)));


            res = ESParse.execQuery(restClient,EMAILSTRING,ENERGYSEARCHURL);
            source.setEmail(ESParse.getTotal(ESParse.getfirsthits(res)));

            res = ESParse.execQuery(restClient,"{}",OUTSOURCEURL);
            source.setOutsource(ESParse.getTotal(ESParse.getfirsthits(res)));
            source.setDibai(source.getOutsource());
            source.setTotal(source.getDibai()+source.getChangping());

            sourcetrueenumService.update(source);

        }



        //secondaryJdbcTemplate.query("select * from t_hot_words",);
        //template.convertAndSend("/topic/getResponse", new Response("欢迎体验bootdo,这是一个任务计划，使用了websocket和quzrtz技术，可以在计划列表中取消，欢迎您加入qq群交流学习!" ));

    }



}